/**
 * OSAmI: Open Source Ambient Intelligence
 * Copyright (C) 2010 Bull S.A.S.
 * Contact: osami-general@osami-commons.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 */

package org.osami.gateway.sensor.data;

import org.osgi.util.measurement.Measurement;

/**
 * Created by IntelliJ IDEA.
 * User: guillaume
 * Date: 29 mai 2010
 * Time: 10:51:34
 * To change this template use File | Settings | File Templates.
 */
public abstract class AbstractSenseData implements SenseData {
    private String id;
    protected String type;
    private double value;
    protected String unit;
    protected String location;

    private Measurement measurement;

    private static final String SEPARATOR = "|";

    public AbstractSenseData(double value, String id, Measurement measurement) {
        this.value = value;
        this.id = id;
        this.measurement = measurement;
    }

    protected AbstractSenseData(String serializedForm) {
        try {
            //System.out.println("in : " + serializedForm);
            String [] strings = serializedForm.split("\\" + SEPARATOR);
            Long timestamp = new Long(strings[0]);
            this.value = new Double(strings[3]);
            this.measurement = new Measurement(this.value, 0.5, null, timestamp);

            this.id = strings[1];
            this.type = strings[2];
            this.unit = strings[4];
            if (strings[5].equals("")) {
                this.location = null;
            }
            else {
                this.location = strings[5];
            }

            //System.out.println("out: " + this.toString());
        }
        catch (Exception e) {
            // Silently swallow error
            e.printStackTrace();
        }
    }


    public Measurement toMeasurement() {
        return measurement;
    }


    public Long getTimeStamp() {
        return measurement.getTime();
    }

    public String getSenseId() {
        return id;
    }

    public String getType() {
        return type;
    }

    public Double getValue() {
        return value;
    }

    public String getUnit() {
        return unit;
    }

    public String getLocation() {
        if (location != null) {
            return location;
        }
        else {
            return "";
        }
    }

    @Override
    public String toString() {
        return getTimeStamp() + SEPARATOR + getSenseId() + SEPARATOR + getType() + SEPARATOR + getValue() + SEPARATOR + getUnit() + SEPARATOR + getLocation();
    }
}
